home *** CD-ROM | disk | FTP | other *** search
- :
- #
- # E-Mail address directory lookup script to read $HOME/.addresses
- #
- # The format of the $HOME/.addresses file is simple:
- # Place one E-Mail address on each line.
- # Descriptive comments that should be displayed but NOT included in
- # the address headers may follow the address, enclosed in [ ].
- #
- # Samples:
- # "Fred Jones" <fjones@jones.com> [President of Jones Inc.]
- # mary@ucla.edu (Mary Johnson)
- # herbert (Herb Smith) [Herb, down in Accounting]
- #
- # The environment variable ADDRESSES can be used to reference another file
- # instead of $HOME/.addresses, in the same format.
- #
- if test -z "$ADDRESSES"
- then
- ADDRESSES=$HOME/.addresses
- fi
- if test $# -ne 2
- then
- echo "`basename $0`: Expected 2 args, got $#"
- exit 3 # Incorrect arguments
- fi
- matches=${TMPDIR-/tmp}/admatch$$
-
- # make SP = space + tab
- SP=`echo x | tr x '\011'``echo x | tr x '\040'`
-
- # extract address part if this doesn't look like it's already a regex
- # this won't work absolutely 100% but it should get it right on
- # non-perverse cases
- addressonly=$2
- if echo "$addressonly" | grep -v '[]\*]' >/dev/null
- then
- # the following sed expressions do this, in order:
- # - assume that anything in double quotes is a comment and drop it
- # - pull out the address from any remaining comments
- # - squeeze adjacent whitespace, strip leading/trailing whitespace
- # - drop anything that looks like a relay/host name; in particular
- # user@host, host!user@relay, user%host@relay, host1!host2!...!user
- addressonly=`echo " $addressonly " \
- | sed -e 's/\([^!]\)"[^"]*"\([^@]\)/\1\2/g' \
- -e 's/([^)]*)//g' -e 's/^.*<\([^>]*\)>.*$/\1/' \
- -e "s/[$SP][$SP]*/ /g" -e "s/^[$SP]//" -e "s/[$SP]$//" \
- -e 's/@.*$//' -e 's/%.*$//' -e 's/^.*!\([^!]*\)$/\1/'`
-
- # if we somehow nuked the entire string, fall back on the original
- # string, stripping one level of quoting if it exists
- test -z "$addressonly" && addressonly=`echo "$2" \
- | sed -e 's/^"\(.*\)"$/\1/'`
- fi
-
- (egrep -i "$addressonly" $ADDRESSES || fgrep -i "$addressonly" $ADDRESSES) > $matches 2>/dev/null
- count=`wc -l < $matches`
- EXIT=1 # Execution failure
- if test $count -eq 0
- then
- echo "$2"
- EXIT=4 # No matches found
- elif test $count -gt $1 -a $1 -gt 0
- then
- echo "Matched $count names (max $1)"
- echo "Use a more specific pattern please."
- EXIT=2 # Too many matches
- else
- cat $matches
- if test $count -eq 1
- then
- EXIT=5 # Exactly one match
- else
- EXIT=0 # At least one match
- fi
- fi
- rm -f $matches
- exit $EXIT
-